﻿/*
Remdator v5.0.0
http://code.google.com/p/remdator/

Copyright (c) 2012 Marc Tanti (www.marctanti.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

;(function ($, undefined) {
if (typeof($.remdator) == "undefined")
{
	$.remdator = {};
}

function TextAreaFormElement($args)
//Text area DOM wrapper.
//$args: {
//	dom: A jQuery object wrapping the <textarea> tag to represent.
//  defaultAjaxOperation: { Default operation parameters to override missing parameters in ajaxOperations.
//		errorChecker($ajaxOutputArgs): The function which checks the output from the remote script to see if it returned an error and throws a RemoteError exception if so.
//			$ajaxOutputArgs: {
//				source: The DataElement object that the operation belongs to.
//				requestData: The jQuery ajax data used to make the ajax request.
//				output: The remote script output.
//			}
//		success($source, $requestData, $output): The function which does something with the output from the remote script if no errors were returned.
//			$ajaxOutputArgs: {
//				source: The DataElement object that the operation belongs to.
//				requestData: The jQuery ajax data used to make the ajax request.
//				output: The remote script output.
//			}
//		settings: Default jQuery ajax settings as specified in http://api.jQuery.com/jQuery.ajax/ (which overrides jQuery defaults) to use when calling a remote script (you cannot override url, data, success or error). If undefined, will use jQuery defaults.
//	}
//	ajaxOperations: { An object where each value, which represents an operation of your choosing, maps to an ajax operation object, creating a new ajax operation.
//		operation name : {
//			url: The url of a remote script to call through ajax for loading or changing remote data.
//			errorChecker($ajaxOutputArgs): The function which checks the output from the remote script to see if it returned an error and throws a RemoteError exception if so.
//				$ajaxOutputArgs: {
//					source: The DataElement object that the operation belongs to.
//					requestData: The jQuery ajax data used to make the ajax request.
//					output: The remote script output.
//				}
//			success($source, $requestData, $output): The function which does something with the output from the remote script if no errors were returned.
//				$ajaxOutputArgs: {
//					source: The DataElement object that the operation belongs to.
//					requestData: The jQuery ajax data used to make the ajax request.
//					output: The remote script output.
//				}
//			settings: Custom jQuery ajax settings as specified in http://api.jQuery.com/jQuery.ajax/ (which extends and overrides defaultAjaxOperation) to use when calling the remote script (you cannot override url, data, success or error).
//		}
//	}
//	defaultFormOperation: { Default operation parameters to override missing parameters in formOperations.
//		errorChecker($formOutputArgs): The function which checks the output from the remote script to see if it returned an error and throws a RemoteError exception if so.
//			$formOutputArgs: {
//				source: The DataElement object that the operation belongs to.
//				form: The form used to submit the request.
//				output: The remote script output.
//			}
//		success($formOutputArgs): The function which does something with the output from the remote script if no errors were returns.
//			$formOutputArgs: {
//				source: The DataElement object that the operation belongs to.
//				form: The form used to submit the request.
//				output: The remote script output.
//			}
//		settings: { Default settings for submitting a form.
//			form: The jQuery wrapper object of the form to submit. Must be available at the time of calling this function.
//			iframe: The iframe to recieve the script's output in. Must be available at the time of calling this function and must have a unique "name" attribute.
//			dataType: The data type (which overrides defaultFormDataType) of the output of the remote script after submitting the form. Can be either "json", "xml" or "text". Default is "text".
//			preSubmit($preSubmitArgs): The function to call immediately before submitting the form (such as to fill in hidden fields).
//				$preSubmitArgs: {
//					$source: The DataElement object that the operation belongs to.
//					$form: The form that will be used to submit the request.
//				}
//		}
//	}
//	formOperations: { An object where each value, which represents an operation of your choosing, maps to a form operation object, creating a new form submission operation.
//		operation name : {
//			url: The url of the remote script to submit the form to.
//			errorChecker($formOutputArgs): The function which checks the output from the remote script to see if it returned an error and throws a RemoteError exception if so.
//				$formOutputArgs: {
//					source: The DataElement object that the operation belongs to.
//					form: The form used to submit the request.
//					output: The remote script output.
//				}
//			success($formOutputArgs): The function which does something with the output from the remote script if no errors were returns.
//				$formOutputArgs: {
//					source: The DataElement object that the operation belongs to.
//					form: The form used to submit the request.
//					output: The remote script output.
//				}
//			settings: { Settings for submitting the form for this operation (which override defaultFormOperation).
//				form: The jQuery wrapper object of the form to submit. Must be available at the time of calling this function.
//				iframe: The iframe to recieve the script's output in. Must be available at the time of calling this function and must have a unique "name" attribute.
//				dataType: The data type of the output of the remote script after submitting the form. Can be either "json", "xml" or "text". Default is "text".
//				preSubmit($preSubmitArgs): The function to call immediately before submitting the form (such as to fill in hidden fields).
//					$preSubmitArgs: {
//						$source: The DataElement object that the operation belongs to.
//						$form: The form that will be used to submit the request.
//					}
//			}
//		}
//	}
//}
{
	if (typeof($args) != "undefined")
	{
		//Inherit from SingleDataElement.
		$.remdator.SingleDataElement.call(this, $.extend({}, $args, { initLoaded : false }));
		
		$.remdator.assertArgType({ arg : $args.dom, name : "dom", type : "object" });
		
		if (!$args.dom.is("textarea"))
		{
			throw new $.remdator.GeneralError({ message : "Incorrect DOM object." });
		}
		
		var $this = this;
		
		//The jQuery object wrapping the DOM element.
		this._dom = $args.dom;
		
		//Add an inversed reference to this object in the DOM element.
		this._dom.data("remdator", this);
		
		this._dom.change( function(){$this.fireChangeEvent();} );
	}
}
TextAreaFormElement.prototype = new $.remdator.SingleDataElement;

/***********************************/
TextAreaFormElement.prototype.getDOM = function()
//Get the jQuery object wrapping the form element.
{
	return this._dom;
};

/***********************************/
TextAreaFormElement.prototype.getValue = function()
//Get the current value.
{
	$.remdator.SingleDataElement.prototype.getValue.call(this);
	
	return this._dom.val();
};

/***********************************/
TextAreaFormElement.prototype.setValue = function($args)
//Set the current value.
//$args: {
//	value: The value to set.
//}
{
	$.remdator.SingleDataElement.prototype.setValue.call(this, $args);
	
	this._dom.val($args.value);
	this.fireChangeEvent();
};

//Allow user to give a particular text area remdator functionalities by calling a method from the text area.
//For example $("text area selector").remdator_textarea({args})
//The TextAreaFormElement object will be returned when calling the remdator_textarea method.
$.fn.remdator_textarea = function($args)
{
	return new TextAreaFormElement($.extend({}, $args, { dom : this }));
};
//Add to remdator namespace in jQuery.
$.remdator.TextAreaFormElement = TextAreaFormElement;
})(jQuery);